package com.example.smartcity.ui.smartCity.activitytModule

import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity.R
import com.example.smartcity.databinding.ActivitySm2ShowBinding
import com.example.smartcity.ui.smartCity.Adapter.UserRecyclerViewAdapter
import com.example.smartcity.ui.smartCity.model.user

class Sm2ShowActivity : AppCompatActivity() {
    private lateinit var mBinding: ActivitySm2ShowBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivitySm2ShowBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        title = "视频详细"
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        // 播放视频
        startVideo()
        // 设置信息
        setNewsTitle()
        // 设置评论设置
        setReView()
    }

    private fun setReView() {
        mBinding.sm2ShowRecyclerView.apply {
            this.layoutManager = LinearLayoutManager(context)
            this.adapter = UserRecyclerViewAdapter(ArrayList<user>().apply {
                for (i in 1..40){
                    val index = (1..10).random()
                    this.add(user(
                        "智慧城市新用户@id14$i",
                        getStr(index)
                    ))
                }
            })
        }


    }

    fun getStr(index: Int) : String{
        val list = listOf<String>(
            "老师非常热情，材料非常及时",
            "材料怎么获取呢？",
            "我想要",
            "敬礼要定格，注意书写规范",
            "我们就得1500个字",
            "傍晚的鼓浪屿",
            "黄厝海滩的日出",
            "别说是假的了 厦门人告诉你每年夏天都可以看到很美的天空",
            "这几天下雨加阴天还有疫情，要来慎重",
            "英语肯定过四级了",
            "说实话，这个情感真的很到位，我个人觉得很震撼"
        )
        return list[index]
    }

    private fun setNewsTitle() {
        val title = intent.getStringExtra("title").toString()
        mBinding.sm2ShowTitle.text = title
        val contend = intent.getStringExtra("contend").toString()
        mBinding.sm2ShowContend.text = contend
        val time = "发布时间:" + intent.getStringExtra("time").toString()
        mBinding.sm2ShowTime.text = time
    }

    private fun startVideo() {
        val uri = Uri.parse("android.resource://$packageName/${R.raw.video}")
        mBinding.sm2ShowVideoView.setVideoURI(uri)
        // 开始播放
        mBinding.sm2ShowVideoView.start()
    }


    override fun onDestroy() {
        super.onDestroy()
        mBinding.sm2ShowVideoView.stopPlayback()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        mBinding.sm2ShowVideoView.stopPlayback()
        this@Sm2ShowActivity.finish()
        return super.onOptionsItemSelected(item)
    }
}